﻿using DoudizhuMsg;
using Frameworks;
using System.Collections.Generic;
using UnityEngine;

namespace Doudizhu
{
    public class DoudizhuData : Singleton<DoudizhuData>
    {
        private DoudizhuData() { }

        public override void OnSingletonInit()
        {
            base.OnSingletonInit();

            players = new List<PlayerEntity>();
            gamePlayers = new List<GamePlayerEntity>();
        }

        public override void Dispose()
        {
            base.Dispose();
        }

        public Player my { get; set; }
        public List<PlayerEntity> players { get; private set; }

        public GamePlayer myGame { get; set; }
        public List<GamePlayerEntity> gamePlayers { get; private set; }

        // -------------------------------------------------

        public PlayerEntity GetPlayerByUid(uint uid)
        {
            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].player.Uid == uid)
                {
                    return players[i];
                }
            }
            return null;
        }

        public void AddPlayer(PlayerEntity player)
        {
            RemovePlayer(player.player);

            players.Add(player);
        }

        public void RemovePlayer(Player player)
        {
            for (int i = 0; i < players.Count; i++)
            {
                if (players[i].player.Uid == player.Uid)
                {
                    Object.Destroy(players[i].go);
                    players.Remove(players[i]);
                    break;
                }
            }
        }

        public void ClearPlayers()
        {
            for (int i = 0; i < players.Count; i++)
            {
                Object.Destroy(players[i].go);
            }
            players.Clear();
        }

        // -------------------------------------------------

        public GamePlayerEntity GetGamePlayerByUid(uint uid)
        {
            for (int i = 0; i < gamePlayers.Count; i++)
            {
                if (gamePlayers[i].gamePlayer.Uid == uid)
                {
                    return gamePlayers[i];
                }
            }
            return null;
        }

        public GamePlayerEntity GetGamePlayerBySeat(int seat)
        {
            for (int i = 0; i < gamePlayers.Count; i++)
            {
                if (gamePlayers[i].gamePlayer.Seat == seat)
                {
                    return gamePlayers[i];
                }
            }
            return null;
        }

        public void AddGamePlayer(GamePlayerEntity player)
        {
            RemoveGamePlayer(player.gamePlayer);

            gamePlayers.Add(player);
        }

        public void RemoveGamePlayer(GamePlayer player)
        {
            for (int i = 0; i < gamePlayers.Count; i++)
            {
                if (gamePlayers[i].gamePlayer.Uid == player.Uid)
                {
                    Object.Destroy(gamePlayers[i].go);
                    gamePlayers.Remove(gamePlayers[i]);
                    break;
                }
            }
        }

        public void ClearGamePlayers()
        {
            for (int i = 0; i < gamePlayers.Count; i++)
            {
                Object.Destroy(gamePlayers[i].go);
            }
            gamePlayers.Clear();
        }
    }
}
